<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php

$start_time=time();
error_reporting(ALL);
	define('DB_HOST', "192.168.1.215:3306");
	define('DB_ROOT', "root");
	define('DB_PASS', "passw0rd");
	define('START_DAY', "2012-10-15");
	define('END_DAY', "2013-04-15");
	$conn = mysql_connect ( DB_HOST, DB_ROOT, DB_PASS );
	// $conn = mysql_connect ( "192.168.1.215:3306", "root", "passw0rd" );
	mysql_query ( "set names utf8" );
	// mysql_select_db ( "phoenix_stag_prd" );
	mysql_select_db("phoenix_stag_prd");
	//$sql="select * from product_calendar where departs_date>'2012-10-16' group by product_id";
	$sql="SELECT s.*,p.* FROM (SELECT * FROM product_calendar WHERE departs_date>NOW() GROUP BY product_id) p LEFT JOIN (SELECT p.id pid,r.res_id,MIN(c.departs_date) min_day,MAX(c.departs_date) max_day FROM product p LEFT JOIN product_journey j ON p.base_id=j.base_id LEFT JOIN product_res r ON j.id=r.journey_id JOIN resource_date_cost c WHERE r.res_id=c.res_id GROUP BY c.res_id) s ON p.product_id=s.pid ";
	$arr=GetPlan($sql,$conn);
	mysql_query("truncate table product_calendar");
	foreach ($arr as $key => $value) {
		$sql_s=array();
		for ($i=START_DAY; $i < END_DAY; $i=date("Y-m-d",strtotime("+1 day",strtotime($i)))) { 
			if($value['max_day']<$i){
				$value['del_flag']=1;
			}
			$sql_s[]="({$value['product_id']},{$value['price']},{$value['stock']},'{$value['op_time']}','{$i}',{$value['is_close']},{$value['cost']},{$value['del_flag']})";
		}
		mysql_query("INSERT INTO product_calendar(product_id,price,stock,op_time,departs_date,is_close,cost,del_flag) VALUES ".implode($sql_s, ","));
		
	}
	// var_dump($sql_s);die();
	
	//echo "INSERT INTO product_calendar(product_id,price,stock,op_time,departs_date,is_close,cost,del_flag) VALUES ".implode($sql_s, ",");
	
$end_time=time();
echo "脚本运行：".($end_time-$start_time)."s";

	function GetPlan($sql, &$conn) {
	
		$rs = mysql_query ( $sql, $conn );
		$data = array ();
		while ( ($row = mysql_fetch_assoc ( $rs )) != false ) {
			$data [] = $row;
		}
		mysql_free_Result ( $rs );
		return $data;
	}
	function GetRow($sql, &$conn) {
		$rs = mysql_query ( $sql, $conn );
		$row = mysql_fetch_assoc ( $rs );
		@mysql_free_Result ( $rs );
		return $row;
	}
	function Query($sql) {
		$res = mysql_query ( $sql );
		return $res;
	}
?>